• time
  • time notation
  • timeseries
  • reading/writing data
  • plotting dates

  • packages, functions, structures inside a .py file


In [1]:
# relevant packages for time
# from the standard library:
import time
import datetime

# external packages
import pytz
import dateutil

In [43]:
# time is based on the c time library
# Return the current time in seconds since the Epoch.
time.time()


Out[43]:
1404751531.032971

In [42]:
?time

In [14]:
# epoch in greenwich:
time.gmtime(0)


Out[14]:
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

In [13]:
# current time in greenwich
time.gmtime()


Out[13]:
time.struct_time(tm_year=2014, tm_mon=7, tm_mday=7, tm_hour=12, tm_min=23, tm_sec=2, tm_wday=0, tm_yday=188, tm_isdst=0)

In [72]:
# object oriented date/time
import datetime
birthday = datetime.datetime(1976, 2, 23)

class Person:
    def __init__(self, name):
        self.name = name
    @staticmethod
    def born():
        person = Person(name=None)
        return person
Person.born()


Out[72]:
<__main__.Person instance at 0x103677d40>

In [45]:
now = datetime.datetime.now()
now


Out[45]:
datetime.datetime(2014, 7, 7, 18, 48, 20, 320138)

In [48]:
# common format
now.isoformat()


Out[48]:
'2014-07-07T18:48:20.320138'

In [50]:
# date notation
now + datetime.timedelta(days=10)


Out[50]:
datetime.datetime(2014, 7, 17, 18, 48, 20, 320138)

In [53]:
import pytz

In [ ]:


In [74]:
# current timezone
tz = pytz.timezone('Europe/Amsterdam')
tz


Out[74]:
<DstTzInfo 'Europe/Amsterdam' LMT+0:20:00 STD>

In [75]:
# local time
tz.localize(now)


Out[75]:
datetime.datetime(2014, 7, 7, 18, 48, 20, 320138, tzinfo=<DstTzInfo 'Europe/Amsterdam' CEST+2:00:00 DST>)

In [79]:
import dateutil.parser

In [85]:
for date in ['23/02/1976', '2000-01-02']:
    print(dateutil.parser.parse(date))


1976-02-23 00:00:00
2000-01-02 00:00:00

In [86]:
datetime.datetime.strptime('03/02/1976', '%d/%m/%Y')


Out[86]:
datetime.datetime(1976, 2, 3, 0, 0)

In [88]:
import dateutil.rrule

In [91]:



Out[91]:
datetime.datetime(2014, 7, 7, 19, 8, 1)

In [92]:
import pandas

In [102]:
values = [1,2,3]
times = [datetime.datetime(2000,1,1), datetime.datetime(2000,1,2), datetime.datetime(2000,1,3)]
ts = pandas.TimeSeries(values, index=times)

In [103]:
import matplotlib.pyplot as plt

In [111]:
fig, ax = plt.subplots()
ax.plot(times, values)
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%d-%m'))
ax.xaxis.set_major_locator(matplotlib.dates.DayLocator())



In [ ]: